/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tetris_invader.windows;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import javazoom.jl.decoder.JavaLayerException;
import tetris_invader.personagens.Fabrica;
import tetris_invader.util.Fase;

/**
 * 
 *
 */
public class Frame extends JFrame {

    protected Fase fase;
    protected JPanel painel;
    protected GameOver gameOver;
    protected InfoFrame info;
    protected CardLayout card;
    protected Principal principal;

    public Frame(boolean mode) throws JavaLayerException {
        setSize(640, 600);
        setTitle("Tetris Invaders");
        painel = new JPanel();
        this.setContentPane(painel);

        card = new CardLayout();
        painel.setLayout(card);

        fase = new Fase(this, mode);
        fase.setName("Fase");
        Menu m = new Menu(this, fase);
        principal = new Principal();

        Acoes listener = new Acoes(fase);

        principal.addListener(listener);
        principal.setFrame(this);
        gameOver = new GameOver();
        gameOver.addListener(listener);
        m.addListener(listener);

        //Coloca a Janela principal no centro da tela
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        int x = d.width / 2 - this.getSize().width * 3 / 5;
        int y = d.height / 2 - this.getSize().height * 3 / 5;
        this.setLocation(x, y);
        this.addWindowListener(new Closer());
        this.addKeyListener(new Teclado());
        this.setResizable(false);
        
        painel.add(principal.painel, "Principal");
        painel.add(fase, "Fase");
        painel.add(gameOver, "GameOver");

        card.show(painel, "Principal");

        this.setJMenuBar(m.menu);

        info = new InfoFrame(this, fase);
        listener.setInfo(info);
       // principal.playMusic();
    }

    /**
     * reinicia o painel da fase e todo o conteudo da mesma.
     */
    public void restart() {
        fase.restart();
        startLevel(false);
    }

    /**
     * Torna visivel o painel da fase, onde ocorre o jogo em si.
     * @param mode
     */
    public void startLevel(boolean mode) {
        principal.pause();

        this.info.setVisible(true);
        card.show(painel, "Fase");
        this.requestFocus();
        fase.start();
        info.start();
        fase.inicializar(mode);
        fase.setInfoPanel(this.info.getInfoPanel());
    }

    /**
     * Deixa visivel o menu Principal.
     */
    public void mainMenu() {
        if (!fase.isPause()) {
            fase.stopFase();
        }

        this.info.setVisible(false);
        card.show(painel, "Principal");
    }

    /**
     * Mostra apenas o painel de Game over, escondendo os demais.
     * Tambem faz um print na tela da quantidade de pontos adquirido durante o jogo.
     * @param score
     */
    public void GameOver(int score) {
        if (!fase.isPause()) {
            fase.stopFase();
        }
        card.show(painel, "GameOver");
        gameOver.gameOver(score);
    }

    public void start() {
        principal.start();
    }

    /*****************************************************************************/
    /*CLASSES INTERNAS ***********************************************************/
    /*****************************************************************************/
    /**
     *  Listener (fechamento da janela)
     */
    public class Closer extends WindowAdapter implements WindowListener {

        @Override
        public void windowClosing(WindowEvent e) {

            int arg = e.getNewState();

            if (arg == 0) {//WindowEvent.WINDOW_CLOSED) {
                int x = JOptionPane.showConfirmDialog(null, "Are you sure???\n"
                        + "Your current data will be lost!",
                        "Exit", JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.WARNING_MESSAGE);
                if (x == JOptionPane.OK_OPTION) {
                    fase.save();
                    System.exit(2);
                } else if (x == JOptionPane.CANCEL_OPTION) {
                    //TODO arrumar o cancelar
                }
            }
        }
    }


    /**
     * Leitura do teclado
     */
    public class Teclado extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_P) {
                fase.pause();
            } else if (e.getKeyCode() == KeyEvent.VK_L) {
                fase.setElim(100000);
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    }

    public class Acoes implements ActionListener {

        private Fase fase;
        private InfoFrame info;

        public Acoes(Fase f) {
            fase = f;
        }

        public void setInfo(InfoFrame info) {
            this.info = info;
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JMenuItem) {
                String arg = e.getActionCommand();

                if (arg.equals("Exit")) {
                    int x = JOptionPane.showConfirmDialog(null, "Are you sure???\n"
                            + "Your current data will be lost!",
                            "Exit", JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.WARNING_MESSAGE);
                    if (x == JOptionPane.OK_OPTION) {
                        System.exit(1);
                    }

                } else if (arg.equals("New Game")) {
                    int x = JOptionPane.showConfirmDialog(null, "You will start a new game!\n"
                            + "Your current data will be lost!",
                            "New Game", JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.WARNING_MESSAGE);
                    if (x == JOptionPane.OK_OPTION) {
                        card.show(getContentPane(), "Fase");
                        startLevel(false);
                        info.start();
                    }
                } else if (arg.equals("Save Game")) {
                    fase.save();
                    JOptionPane.showMessageDialog(null, "Game Saved!", "Save Game",
                            JOptionPane.WARNING_MESSAGE);
                } else if (arg.equals("Load Game")) {
                    fase.recover();
                    fase.setInfoPanel(info.getInfoPanel());
                    JOptionPane.showConfirmDialog(null, "You current game will be lost.\n"
                            + "Do yo want to continue?", "Load Game",
                            JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
                } else if (arg.equals("How to Play")) {
                    JOptionPane.showConfirmDialog(null, "Use the direcional pad to control"
                            + " your nave.\nBe careful with the invaders and destruct"
                            + " them firing with space bar!\n Note the energy bar in the Info Panel."
                            + "When it get full, you can fire a special beam pressing Z button!\n\n"
                            + "Good Game!",
                            "How to Play",
                            JOptionPane.OK_OPTION,
                            JOptionPane.INFORMATION_MESSAGE);
                } else if (arg.equals("Credits")) {
                    JOptionPane.showConfirmDialog(null, "Created by:\n"
                            + "Rafael Umino Nakanishi and\n"
                            + "Tamires Harumi Dantas Takehara\n\n"
                            + "University of Sao Paulo\n"
                            + "Summer 2010", "Credits",
                            JOptionPane.OK_OPTION,
                            JOptionPane.INFORMATION_MESSAGE);
                } else if (arg.equals("Main Menu")) {
                    int opt = JOptionPane.showConfirmDialog(null, "You will go to Main Menu!\n"
                            + "Current game will be lost. Do you want to save?", "Main Menu",
                            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);

                    if (opt == JOptionPane.YES_OPTION) {
                        fase.save();
                        if (!fase.isPause()) {
                            fase.stopFase();
                        }
                        mainMenu();
                    }
                }
            } else if (e.getSource() instanceof JButton) {
                String s = e.getActionCommand();

                if (s.equals("Restart")) {
                    card.show(painel, "Fase");

                    startLevel(true);
                    info.start();

                } else if (s.equals("Exit")) {
                    int x = JOptionPane.showConfirmDialog(null, "Are you sure???\n",
                            "Exit", JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.WARNING_MESSAGE);
                    if (x == JOptionPane.OK_OPTION) {
                        System.exit(1);
                    }

                } else if (s.equals("Main Menu")) {
                    mainMenu();

                } else if (s.equals("New Game")) {
                    startLevel(false);
                    info.start();

                } else if (s.equals("Load Game")) {
                    fase.recover();
                    fase.setInfoPanel(this.info.getInfoPanel());
                    card.show(painel, "Fase");
                    fase.start();
                } else if (s.equals("Help")) {

                    JOptionPane.showConfirmDialog(null, "Use the direcional pad to control"
                            + " your nave.\nBe careful with the invaders and destruct"
                            + " them firing with space bar!\n Note the energy bar in the Info Panel."
                            + "When it get full, you can fire a special beam pressing Z button!\n\n"
                            + "Good Game!",
                            "How to Play",
                            JOptionPane.OK_OPTION,
                            JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    }
}
